import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

// 引入 首页
import buttonEle from "../views/home.vue";

// 分类页面
import detCal from "../views/cla.vue";
// 分类页面内容
import contentEle from "../components/detCla/content.vue";

// 购物车页面
import shoppingCar from "../views/shoppingCar.vue";

// 我的 页面
import myHome from "../views/myHome.vue";

// 商品详情列表
import detGoods from "../views/detGoods.vue";

// 商品详情
import goodsEle from "../views/goods.vue";

// 登录页面
import loginPage from "../views/login.vue";

// 账号管理
import userMag from "../views/userMag.vue";

// 注册页面
import register from "../views/register.vue";

// qq回调页面
import proXy from "../views/qqPage.vue";

const routes = [
  // 首页
  {
    path: "/",
    name: "buttonEle",
    component: buttonEle,
    meta: {
      index: 1,
    },
  },
  // 分类页
  {
    path: "/detCal",
    name: "detCal",
    component: detCal,
    redirect: "/content",
    children: [{ path: "/content", name: "contentEle", component: contentEle }],
    meta: {
      index: 1,
    },
  },
  // 购物车
  {
    path: "/shoppingCar",
    name: "shoppingCar",
    component: shoppingCar,
    meta: {
      index: 1,
      auto: true,
    },
  },
  // 我的页面
  {
    path: "/myHome",
    name: "myHome",
    component: myHome,
    meta: {
      index: 1,
      auto: true,
    },
  },
  // 商品列表页面
  {
    path: "/detGoods",
    name: "detGoods",
    component: detGoods,
    meta: {
      index: 2,
    },
  },
  // 商品详情页
  {
    path: "/goodsEle:id",
    name: "goodsEle:index",
    component: goodsEle,
    meta: {
      index: 3,
      auto: true,
    },
  },
  // 登录页面
  {
    path: "/loginPage",
    name: "loginPage",
    component: loginPage,
    meta: {
      index: 2,
    },
  },
  // 管理用户页
  {
    path: "/userMag",
    name: "userMag",
    component: userMag,
    meta: {
      index: 2,
      auto: true,
    },
  },
  // 注册页面
  {
    path: "/register",
    name: "regiSter",
    component: register,
    meta: {
      index: 3,
    },
  },
  // qq回调页面
  {
    path: "/proxy",
    name: "proXy",
    component: proXy,
    meta: {
      index: 3,
    },
  },
];

const router = new VueRouter({
  mode: "history",
  routes,
});

router.beforeEach((to, from, next) => {
  if (to.meta.auto) {
    // 获取 token
    let token = window.sessionStorage.getItem("token");
    if (token) {
      next();
    } else {
      next("/loginPage");
    }
  } else {
    next();
  }
});

export default router;
